package com.graduate.daydayup.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduate.daydayup.R;


/**
 * @Author: 张鹏飞
 * @Email: 1271396448@qq.com
 * <p>
 * @Desc: Activity基类
 */
public abstract class BaseActivity
        extends AppCompatActivity {


    ImageView iv_left;
    LinearLayout llcontainer;
    TextView tv_right;
    TextView tv_center_title;
    RelativeLayout rl_toolbar;
    @SuppressLint("WrongConstant")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        setContentView(R.layout.addtitlelayout);
        BarUtils.setStatusBarLightMode(this,true);
        initTopView();
        getView(getLayout());

        attachView();
        initStatusBar();
        initViewIds();

        initView();

        if(addToolbar(isAddToolbar())){
            setCenterTitle(setTitle());
            setRight(setTextRight());
            setRightListener();
        }
        try {
            initCache();
        } catch (Exception ignored) {

        }
        initData(savedInstanceState);
        initListener();
        PermissionUtils.permission(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.BLUETOOTH,
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, PermissionConstants.STORAGE)
                .callback(new PermissionUtils.SimpleCallback() {
                    @Override
                    public void onGranted() {
                    }

                    @Override
                    public void onDenied() {
                        ToastUtils.showShort("未授权存储权限");
                    }
                })
                .request();
//        if (useEventBus())
//            EventBus.getDefault().register(this);
    }

    private void initTopView(){
        llcontainer=findViewById(R.id.llcontainer);
        rl_toolbar=findViewById(R.id.rl_toolbar);
        iv_left=findViewById(R.id.iv_left);
        tv_right=findViewById(R.id.tv_right);
        tv_center_title=findViewById(R.id.tv_center_title);
        iv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public boolean addToolbar(boolean istoobar){
        if (rl_toolbar!=null){
            if (istoobar){
                rl_toolbar.setVisibility(View.VISIBLE);
                return true;
            }else            rl_toolbar.setVisibility(View.GONE);
            return false;
        }
        return false;
    }

   public void setCenterTitle(String title){
        if (tv_center_title!=null){
            tv_center_title.setText(title);
        }
   }

  public void setRight(String resId){
        if(tv_right!=null){
            tv_right.setText(resId);
        }
  }

  public void setRightListener(){
        if(tv_right!=null){
            tv_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setImgRightListener();
                }
            });

        }
  }


    public  void getView(int resId){
        View view= LayoutInflater.from(this).inflate(resId,null);
        llcontainer.addView(view);
    }


    public abstract int getLayout();

    public abstract String setTitle();

    public abstract String setTextRight();

    public abstract void setImgRightListener();

    public abstract boolean isAddToolbar();

    public void attachView(){};

    public void initStatusBar(){};

    public abstract void initViewIds();

    public abstract void initView();

    public void initCache() {
    }

    ;

    public abstract void initData(@Nullable Bundle savedInstanceState);

    public void initListener() {
    }

    /**
     * 是否使用eventbus
     * 默认不使用注册
     *
     * @return: false
     */
    public boolean useEventBus() {
        return false;
    }

    /**
     * 是否允许多指触控
     * 默认不允许
     *
     * @return: true
     */
    public boolean useMorePoints() {
        return false;
    }

    /**
     * 是否允许当前点击输入框,再次点击空白处,隐藏软键盘
     * 默认允许
     *
     * @return true
     */
    public boolean useHiddenSoftInput() {
        return true;
    }

    /**
     * 分发处理activity的事件
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!useMorePoints()) {//不允许多指触控, 默认允许
            if (ev.getPointerCount() > 1) {
                return true;
            }
        }
        if (useHiddenSoftInput()) {//是否启用
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                //当isShouldHideInput(v, ev)为true时，表示的是点击输入框区域，则需要显示键盘，同时显示光标，反之，需要隐藏键盘、光标
                if (isShouldHideInput(v, ev)) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        assert v != null;
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                }
                return super.dispatchTouchEvent(ev);
            }
            // 必不可少，否则所有的组件都不会有TouchEvent了
            try {
                if (getWindow().superDispatchTouchEvent(ev)) {
                    return true;
                }
            } catch (Exception ignored) {
            }
            return onTouchEvent(ev);
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

    /**
     * 是否隐藏输入
     *
     * @param v
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if ((v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            return !(event.getX() > left) || !(event.getX() < right)
                    || !(event.getY() > top) || !(event.getY() < bottom);
        }
        return false;
    }

    /**
     * 设置状态栏颜色
     *
     * @param color: 颜色
     */
    public void setStatusBarColor(@ColorInt int color) {
        BarUtils.transparentStatusBar(this);
    }


}
